function jQuery(arg) {
	this.elements = [];
	// $("div")  $(dom)  $(function(){})
	if (typeof arg == "string") {
		//相当于执行选择器功能  如：jQuery("#main")
		this.elements = this.getEl(arg);
		console.log(this.elements);
	} else if (typeof arg == "function") {
		//相当于执行载入函数 如 jQuery(function(){})
		this.ready(arg);
	}
};
jQuery.prototype.ready = function(arg){
	
};
jQuery.prototype.click = function(fn) {
	for (var i = 0; i < this.elements.length; i++) {
		this.addEvent(this.elements[i],"click",fn);
	}
};
jQuery.prototype.first = function() {
	return this.elements[0];
};
jQuery.prototype.log = function(str) {
	window.console && console.log(str);
};
jQuery.prototype.trim = function(str) {
	return str.replace(/^\s+|\s+$/g, "");
};
jQuery.prototype.toUpper = function(sName) {
	if (/-/.test(sName)) {
		var reg = /-[a-z]/;
		sName = sName.replace(reg, function(s) {
			return s.charAt(1).toUpperCase();
		})
	};
	return sName;
};
jQuery.prototype.getStyle = function(ele, sName) {
	if (ele.currentStyle) {
		return ele.currentStyle[sName];
	} else {
		return getComputedStyle(ele, null)[sName];
	}
};
jQuery.prototype.addPx = function(sName, sVal) {
	var arr = ["width", "height", "left", "top", "bottom", "right"];
	var reg = /^\d+$/;
	for (var i = 0; i < arr.length; i++) {
		console.log(reg.test(sVal))
		if (arr[i] == sName && reg.test(sVal)) {
			return sVal + "px";
		}
	}
	return sVal;
};
jQuery.prototype._setStyle = function(sName, sVal) {
	var _sVal = this.addPx(sName, sVal);
	for (var i = 0; i < this.elements.length; i++) {
		this.elements[i].style[sName] = _sVal;
	}
};
jQuery.prototype.css = function() {
	if (arguments.length == 1) {
		var arg = arguments[0];
		//可能是在获取CSS样式 也可能是通过json的方式设置样式
		if (typeof arg == "string") {
			//获取background-color backgroundColor
			var sName = this.toUpper(arg);
			return this.getStyle(this.elements[0], sName);
		} else {
			//JSON格式样式设置
			for (var key in arg) {
				this._setStyle(key,arg[key]);
			}
		}
	} else {
		//单样式设置 css("background","red");
		this._setStyle(arguments[0],arguments[1]);
	}
};
jQuery.prototype.addEvent = function(obj,eName,fn){
	if(obj.addEventListener){
		obj.addEventListener(eName,fn,false);
	}else{
		obj.attachEvent("on"+eName,fn);
	}
}
jQuery.prototype.on = function(eName,fn){
	for(var i=0;i<this.elements.length;i++){
		this.addEvent(this.elements[i],eName,fn);
	}
}
//jQuery.fn = jQuery.prototype;//定义一个便捷的名字 不是必须 是偷懒行为
jQuery.prototype.getEl = function(sel) {
	sel = this.trim(sel);
	var parents = [];
	var childrens = [document];
	sel = sel.replace(/\s+/g, " ");
	sel = sel.split(" ");
	// "#main span" "div span" 
	for (var m = 0; m < sel.length; m++) {
		parents = childrens;
		childrens = [];
		for (var i = 0; i < parents.length; i++) {
			if (sel[m].charAt(0) == "#") {
				this.log("ID选择器");
				var _sel = sel[m].substring(1);
				this.log(parents[i])
				var ele = document.getElementById(_sel);
				childrens.push(ele);
			} else if (sel[m].charAt(0) == ".") {
				this.log("class选择器");
				var _sel = sel[m].substring(1);
				var eles = this.byClass(parents[i], _sel);
				for (var k = 0; k < eles.length; k++) {
					childrens.push(eles[k]);
				}
				//						childrens = eles;
			} else {
				//属性选择器 如：div[name=...] div[name]
				if (/\[/.test(sel[m])) {
					this.log("标签-属性选择器");
					childrens = this.byAttr(parents[i], sel[m]);
				} else if (/\.|#/.test(sel[m])) { //组合选择器 如：div.wrap 选择class为wrap的div  div#main
					this.log("标签-组合选择器")
				} else { //纯标签选择器  div    span 等
					this.log("纯标签选择器");
					var eles = parents[i].getElementsByTagName(sel[m]);
					for (var k = 0; k < eles.length; k++) {
						childrens.push(eles[k]);
					}
				}
			}
		}
	}
	return childrens;
};
jQuery.prototype.byClass = function(parent, sel) {
	//			IE8及以下不支持getElemntsByClassName方法，所以处理一下兼容性
	if (parent.getElementsByClassName) {
		//如果支持byClassName方法   直接返回选择到的元素
		return parent.getElementsByClassName(sel);
	}
	//获取元素下所有元素的方法 （包括深层次的元素）
	//IE8及以下的获取方法
	var eles = parent.getElementsByTagName("*");
	var reg = new RegExp("\\b" + sel + "\\b");
	var arr = []; //用于存放匹配到的元素
	for (var i = 0; i < eles.length; i++) {
		//得到每一个元素 得到每一个元素的class名称
		var ele = eles[i],
			cName = ele.className;
		// "wrap" "wrap wrap2"
		if (reg.test(cName)) {
			arr.push(ele);
		}
	}
	return arr;
};
jQuery.prototype.byAttr = function(parent, sel) {
	sel = sel.substring(0, sel.length - 1); //div[name
	sel = sel.split("["); //sel = [div,name];
	var parents = parent.getElementsByTagName(sel[0]);
	var arr = [];
	for (var i = 0; i < parents.length; i++) {
		var p = parents[i];
		if (/\*/.test(sel[1])) { //如果是 [name*=wrap]
			this.log("*=");
			var _sel = sel[1].split("*=");
			//_sel[1] = wrap 构建正则匹配wrap的单词边界
			var reg = new RegExp("" + _sel[1] + "");
			//如果能够匹配到
			if (reg.test(p.getAttribute(_sel[0]))) {
				arr.push(p);
			}
		} else if (/\^/.test(sel[1])) { // [name^=wrap]
			log("^=")
		} else if (/\$/.test(sel[1])) { // [name$=wrap]
			log("$=")
		} else { // [name]
			log("只有属性")
		}
	}
	return arr;
};

function $(arg) {
	return new jQuery(arg);
}